/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.chart.property;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JSplitPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import twaver.Element;
import twaver.Node;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.chart.BarChart;
import twaver.chart.LineChart;
import twaver.chart.PieChart;
import twaver.table.TPropertySheet;
import demo.DemoPane;

public class PropertyDemo extends DemoPane{
	
	static{
		TWaverUtil.registerBeanInfoWithoutDefault(ChartElement.class);
		TWaverUtil.registerBeanInfo(ChartElement.class, "/demo/chart/property/Chart.xml");
		TWaverUtil.registerBeanInfo(ChartScaleElement.class, "/demo/chart/property/Scale.xml");
		TWaverUtil.registerBeanInfo(ChartScaleLineElement.class, "/demo/chart/property/Line.xml");
	}
	
	private TDataBox box = new TDataBox();
	private BarChart barChart = new BarChart(box);
	private PieChart pieChart = new PieChart(box);
	private LineChart lineChart = new LineChart(box){
		public Color getAreaColor(Element element){
			if(element == red){
				return null;
			}else{
				return super.getAreaColor(element);
			}
		}
	};
	
	Element red = new Node();
	Element green = new Node();
	Element blue = new Node();
	
	public PropertyDemo(){
		lineChart.setLineType(TWaverConst.LINE_TYPE_AREA);
		lineChart.setUpperLimit(128);
		lineChart.setLowerLimit(-128);
		lineChart.setXAxisValue(0);
		lineChart.setInterruptedWhenNullValue(true);
		
		red.setName("Red");
		green.setName("Green");
		blue.setName("Blue");
		
		barChart.setBackgroundVisible(true);
		pieChart.setBackgroundVisible(true);
		lineChart.setBackgroundVisible(true);
		
		red.putChartColor(Color.RED);
		green.putChartColor(Color.GREEN);
		blue.putChartColor(Color.BLUE);
		
		red.putChartValue(91);
		green.putChartValue(73);
		blue.putChartValue(192);
		
		red.addChartValue(red.getChartValue() - 128);
		green.addChartValue(green.getChartValue() - 128);
		blue.addChartValue(blue.getChartValue() - 128);
		
		box.addElement(red);
		box.addElement(green);
		box.addElement(blue);
		
		GridLayout layout = new GridLayout(3, 1);
		layout.setVgap(10);
    	JPanel chartPane = new JPanel(layout);
    	
    	TPropertySheet sheet = new TPropertySheet();
    	sheet.setEditable(true);
    	Element element = new ChartPieElement();
    	element.setBusinessObject(pieChart);
    	sheet.setElement(element);
    	JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pieChart, new JScrollPane(sheet));
    	splitPane.setDividerLocation(400);
    	chartPane.add(splitPane);
    	
    	sheet = new TPropertySheet();
    	sheet.setEditable(true);
    	element = new ChartScaleElement();
    	element.setBusinessObject(barChart);
    	sheet.setElement(element);
    	splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, barChart, new JScrollPane(sheet));
    	splitPane.setDividerLocation(400);
    	chartPane.add(splitPane);

    	sheet = new TPropertySheet();
    	sheet.setEditable(true);
    	element = new ChartScaleLineElement();
    	element.setBusinessObject(lineChart);
    	sheet.setElement(element);
    	splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, lineChart, new JScrollPane(sheet));
    	splitPane.setDividerLocation(400);
    	chartPane.add(splitPane);	
    	
        JPanel sliderPane = new JPanel(new GridLayout(3, 1));  
        final JSlider rSlider = new JSlider(0, 255, (int)red.getChartValue());
        final JSlider gSlider = new JSlider(0, 255, (int)green.getChartValue());
        final JSlider bSlider = new JSlider(0, 255, (int)blue.getChartValue());
        
        sliderPane.add(rSlider);
        sliderPane.add(gSlider);
        sliderPane.add(bSlider);
        
        this.add(chartPane, BorderLayout.CENTER);
        this.add(sliderPane, BorderLayout.SOUTH);

        rSlider.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent e) {
				int value = rSlider.getValue();
				red.putChartValue(value);
				
				red.addChartValue(value-128);
				green.addChartValue(null);
				blue.addChartValue(null);
				
		        if (lineChart.valueCount() > 200) {
		        	lineChart.removeHead(1);
		        }
			}
        });
        gSlider.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent e) {
				int value = gSlider.getValue();
				green.putChartValue(value);
				
				red.addChartValue(null);
				green.addChartValue(value-128);
				blue.addChartValue(null);
				
		        if (lineChart.valueCount() > 200) {
		        	lineChart.removeHead(1);
		        }
			}
        });
        bSlider.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent e) {
				int value = bSlider.getValue();
				blue.putChartValue(value);
				
				red.addChartValue(null);
				green.addChartValue(null);
				blue.addChartValue(value-128);
				
		        if (lineChart.valueCount() > 200) {
		        	lineChart.removeHead(1);
		        }
			}
        });
        
        
        rSlider.setValue(100);
        rSlider.setValue(120);
        rSlider.setValue(150);
        
        gSlider.setValue(200);
        gSlider.setValue(220);
        gSlider.setValue(250);
        
        bSlider.setValue(0);
        bSlider.setValue(20);
        bSlider.setValue(50);
	}
	
}